﻿//--------------------------------------
// Copyright     : W.A.I.S. TECHNOLOGY 2003-2008 ; http://www.wais-fr.com ; email: infos@wais-fr.com
// Author        : Henri d'Orgeval
// Contributors  : 
// Last release  : 2007/11/15 (yy/mm/dd)
// Version       : 1.0.0.1
// Pattern       : "Real Time Trace"
// Internal name : RealTimeTrace Info
// Description   : this class is a sample to show how to implement the RealtimeTrace framework.
//                 
// Framework     : v2.0.50727 and above
//
// Licence       : Microsoft Permisse Licence
// W.A.I.S provides this code in the hope that it will be useful, but WITHOUT ANY WARRANTY , either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.          
// This library is free software; you can redistribute it and/or modify it under the terms of the Microsoft Permissive Licence
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//---------------------------------------


using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using RealtimeTrace; 

namespace RealtimeTrace.Samples
{
    /// <summary>
    /// Sample class to show how to implement the RealtimeTrace framework.
    /// Implementing the Itraceable interface is not mandatory.
    /// Implementing the Itraceable interface will enable you to bubble up any error up to the UI layer.
    /// </summary>
    public class HowToImplement : Itraceable
    {

        #region Itraceable Members

        /// <summary>
        /// Use this method to initialize the RealTimeTrace.
        /// This method should be called within the class constructor(s) 
        /// if you want to override the default RealtimeTrace configuration.
        /// </summary>
        private void InitializeRealTimeTrace()
        {
            this.RealTimeTraceProvider = DataProviderType.LogFile;
            this.PreferredDumpType = DumpType.XML;
        }

        private DataProviderType _RealTimeTraceProvider = DataProviderType.LogFile;
        /// <summary>
        /// Gets or Sets the Data Store provider for the RealTimeTrace.
        /// The default Data Storage provider is a raw text file.
        /// For Windows application, this raw text file is stored at the same level of the running executable.
        /// For ASP.NET application, this raw text file is stored at the same level of the aspx page.
        /// For UI Components, this raw text file is stored in the "My Documents" user folder.
        /// By default the name of the log file is *RealtimeTrace.log*
        /// </summary>
        public DataProviderType RealTimeTraceProvider
        {
            get
            {
                return this._RealTimeTraceProvider;
            }
            set
            {
                this._RealTimeTraceProvider = value;
            }
        }

        private DumpType _PreferredDumpType = DumpType.XML;
        /// <summary>
        /// Gets or Sets the type of dump for the current class.
        /// </summary>
        public DumpType PreferredDumpType
        {
            get
            {
                return this._PreferredDumpType;
            }
            set
            {
                this._PreferredDumpType = value;
            }
        }

        private bool _HasErrors = false;
        /// <summary>
        /// Checks if an error has occured within this class instance.
        /// The Itraceable implementer should only read this property.
        /// This property is set only by the RealtimeTrace layer.
        /// </summary>
        public bool HasErrors
        {
            get { return this._HasErrors; }
            set { this._HasErrors = value; }
        }

        private System.String _LastError = System.String.Empty;
        /// <summary>
        /// Gets the last known error from the last call of a public/private member within this class instance.
        /// The Itraceable implementer should only read this property.
        /// This property is set only by the RealtimeTrace layer.
        /// </summary>
        public string LastError
        {
            get { return this._LastError; }
            set
            {
                if (value == null)
                {
                    value = System.String.Empty;
                }
                this._LastError = value;
            }
        }

        private System.String _Errors = System.String.Empty;
        /// <summary>
        /// Gets all errors from the last call of a public member within this class instance.
        /// The Itraceable implementer should only read this property.
        /// This property is set only by the RealtimeTrace layer.
        /// </summary>
        public string Errors
        {
            get { return this._Errors; }
            set
            {
                if (value == null)
                {
                    value = System.String.Empty;
                }
                this._Errors = value;
            }
        }

        #endregion

        #region Sample methods

        /// <summary>
        /// Sample method to show how to implement the RealtimeTrace framework. 
        /// </summary>
        /// <returns>Returns true if execution of the method has succeeded.
        /// Returns false otherwise.</returns>
        public bool SampleMethod01()
        {
            try
            {
                // implement your code

                // simulate an error during execution
                throw new System.ApplicationException("Error in SampleMethod01");
                
                return true;
            }
            catch (Exception ex)
            {
                TraceFactory.InsertTrace(this, ex);
                return false;
            }
        }

        /// <summary>
        /// Sample method to show how to implement the RealtimeTrace framework. 
        /// </summary>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <returns>Returns true if execution of the method has succeeded.
        /// Returns false otherwise.</returns>
        public bool SampleMethod02(System.Object param1, System.Object param2)
        {
            try
            {
                // implement your code

                // simulate an error during execution
                throw new System.ApplicationException("Error in SampleMethod02");
                
                return true;
            }
            catch (Exception ex)
            {
                TraceFactory.InsertTrace(this, ex,param1,param2);
                return false;
            }
        }

        /// <summary>
        /// Sample method to show how to implement the RealtimeTrace framework. 
        /// </summary>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <returns>Returns true if execution of the method has succeeded.
        /// Returns false otherwise.</returns>
        public bool SampleMethod03(System.Object param1, System.Object param2)
        {
            int i = 0;
            try
            {
                for ( i = 0; i < 1000; i++)
                {
                    // implement your code
                    if (i == 500)
                    {
                        // simulate an error during execution
                        throw new System.ApplicationException("Error in SampleMethod03");
                    } 
                }
                
                return true;
            }
            catch (Exception ex)
            {
                TraceFactory.InsertTrace(this, ex, param1, param2,"i", i);
                return false;
            }
        }


        #endregion


        #region Sample Properties

        private int _SampleProperty01 = 0;
        /// <summary>
        /// Gets or Sets something. 
        /// Sample property to show how to implement the RealtimeTrace framework. 
        /// </summary>
        public int SampleProperty01
        {
            get
            {
                try
                {
                    // implement your code that will compute the associated internal member (_SampleProperty01)
                    return this._SampleProperty01;
                }
                catch (Exception ex)
                {
                    TraceFactory.InsertTrace(this, ex);
                    // Uncomment the following line if you want to bubble up the Exception
                    //throw;
                    return this._SampleProperty01;
                }
            }
            set
            {
                try
                {
                    // Code that checks input value
                    // simulate an error during execution
                    throw new System.ApplicationException("Error in SampleProperty01 in pre-processing step");
                
                    this._SampleProperty01 = value;

                    // Code that will post-process input value;
                }
                catch (Exception ex)
                {
                    TraceFactory.InsertTrace(this, ex,value);
                    // Uncomment the following line if you want to bubble up the Exception
                    //throw;
                }
            }
        }

        #endregion


        #region Constructors

        /// <summary>
        /// Default constructor.
        /// </summary>
        public HowToImplement()
        {
            // Uncomment the following line if you want to override the default RealtimeTrace configuration.
            //InitializeRealTimeTrace();
        }
        #endregion

    }
}
